<?php
namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use app\common\model\Cars;
use app\common\request\Curl;
use app\common\model\CarInfo;

class CarsInfo extends Command
{
    protected function configure()
    {
        $this->setName('cars')->setDescription('车辆数据获取');
    }

    protected function execute(Input $input, Output $output)
    {
        CarInfo::field('car_unique_id')->chunk(100,function($cars){
            $curl = new Curl; 
            $header = config('car_hear.header');
            $url = config('car_hear.url');
            $data['car_unique_id_arr'] = array_column($cars,'car_unique_id');
        	$res = $curl->set('CURLOPT_HTTPHEADER',$header)->post($data)->url($url);
    		 // 任务结果状态
            if ($curl->error()) {
               var_dump($curl->message());exit();
            } else {
                // 任务进程信息
                $info = $curl->info();
                
                if($info['http_code'] == 500){   
                    echo '对方服务器错误';exit();
                }else{
                    // 任务结果内容
                    $content = $curl->data();
                }
            }
            $arr = json_decode($content,true);
            $model = new CarInfo;
            $array = array_values($arr['data']['data']);
            $model->allowField(true)->saveAll($array);
        }); 
    	// $output->writeln('获取了'.$content.'条数据');
    }


}